<template>
  <el-dialog :title="title" :visible.sync="isShow" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" @open="openEvent">
    <el-form :model="form">
      <el-row>
        <el-col :span="8">
          <el-form-item label="菜单名称" :label-width="formLabelWidth">
            <el-input v-model="form.menuName" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="图标" :label-width="formLabelWidth">
            <el-input v-model="form.icon" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="排序" :label-width="formLabelWidth">
            <el-input v-model="form.sort" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="父级菜单" :label-width="formLabelWidth">
            <!-- <el-select v-model="form.parentId" popper-max-height="200px">
              <el-option v-for="item in menuTree" :key="item.id" :label="item.menuName" :value="item.id">
                <span slot="label">{{ item.menuName }}</span>
                <el-tree :data="menuTree" node-key="id" :default-expand-all="true" :props="{ label: 'menuName', children: 'children' }">
                </el-tree>
              </el-option>
            </el-select> -->
            <ParentMenuComp :props="props" :options="menuTree" :value="form.parentId" @getValue="changeParentMenuId" />
            <!-- <ParentMenuComp :parentId="form.parentId" :menuTree="menuTree" @setParentId="changeParentMenuId" /> -->
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="类别" :label-width="formLabelWidth">
            <el-select v-model="form.type" placeholder="请选择">
              <el-option v-for="item in menuType" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="状态" :label-width="formLabelWidth">
            <el-select v-model="form.status" placeholder="请选择">
              <el-option v-for="item in menuStatus" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="路由地址" :label-width="formLabelWidth">
            <el-input v-model="form.path" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="组件" :label-width="formLabelWidth">
            <el-input v-model="form.component" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="参数" :label-width="formLabelWidth">
            <el-input v-model="form.queryParam" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="operateAppInfo">确 定</el-button>
    </div>
  </el-dialog>
</template>
      
<script>
import { getMenuTree, addMenu, updateMenu } from '@/api/menu'
import ParentMenuComp from './parentmenucomp.vue';
export default {
  name: 'CompanyInfoComp',
  components: {
    ParentMenuComp
  },
  data () {
    return {
      props: {
        // 配置项（必选）
        value: "id",
        label: "menuName",
        children: "children",
        // disabled:true
      },
      form: {
        menuName: '',
        parentId: '0',
        type: 'menu',
        path: "",
        component: "",
        queryParam: "",
        icon: "",
        status: "show",
        sort: 0
      },
      menuTree: [],
      formLabelWidth: '100px',
      title: '新增菜单',
      menuType: [{ value: "menu", label: "菜单" }, { value: "page", label: "页面" }, { value: "button", label: "按钮" }],
      menuStatus: [{ value: "show", label: "显示" }, { value: "hidden", label: "隐藏" }]
    }
  },
  props: {
    isShow: {
      type: Boolean,
      required: true
    },
    item: {
      type: Object,
      require: false
    }
  },
  methods: {
    openEvent () {
      if (this.item === undefined) {
        this.form.menuName = ""
        this.form.parentId = "0"
        this.form.type = "menu"
        this.form.path = ""
        this.form.component = ""
        this.form.queryParam = ""
        this.form.icon = ""
        this.form.status = "show"
        this.form.sort = 0
        this.title = '新增菜单'
      } else {
        this.title = '编辑菜单'
        this.form = { ...this.item }
      }
      this.getList()
    },
    getList () {
      const param = {}
      getMenuTree(param).then((res) => {
        if (res.status === 200) {
          const rootMenu = { id: '0', menuName: '根目录', children: res.data }
          this.menuTree = [rootMenu]
        } else {
          this.$message(res.message);
        }
      });
    },
    closeDialog () {
      this.$emit("changeDialogStatus", false);
    },
    result (data) {
      this.$emit("changeDialogStatus", "success");
      if (data.status === 200) {
        this.$message.success("操作成功");
      } else {
        this.$message.error("操作失败，请重试");
      }
    },
    changeParentMenuId (value) {
      this.form.parentId = value;
    },
    operateAppInfo () {
      const param = { ...this.form }
      if (this.item === undefined) {
        addMenu(param).then((res) => {
          this.result(res)
        });
      } else {
        updateMenu(param).then((res) => {
          this.result(res)
        });
      }
    }
  }
}
</script>
      
<style scoped lang="scss">
</style>
      


